package com.itany.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Author：汤小洋
 * Date：2022-11-11 9:32
 * Description：<描述>
 */
@WebListener
public class OnlineListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent e) {
        ServletContext application = e.getSession().getServletContext();

        Object obj = application.getAttribute("num");
        if(obj == null){
            application.setAttribute("num",1);
        }else{
            application.setAttribute("num", (Integer)obj + 1);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent e) {
        ServletContext application = e.getSession().getServletContext();

        int num = (int) application.getAttribute("num");
        application.setAttribute("num", num-1);
    }
}
